<?php
namespace app\index\controller;
use app\index\model\Owner;
use app\index\model\Manager;
use app\index\model\Block;
use think\Controller;
use think\facade\Request;

class Index extends Controller
{

    public function _empty()
    {
        echo "访问地址不存在";
        $this->redirect('http://xj.zzyqt.com/yw/xj');
    }

    // public function test(){
    //     echo 'aaaaa';
    // }

    // public function hello($name = 'ThinkPHP5')
    // {
    //     return 'hello,' . $name;
    // }
    public function enter(Request $request){
        session(null);
        if(!session('block_id') && !session('manager_id')){
            $alias=input('alias');
            if(!$alias){
                $alias='xj';
            }
            $block=Block::where(['alias' => $alias])->find();
            if(!$block){
                $block=Block::get(1);
            }
            $block=$block->toArray();
            $group_id = $block['info']['groups'][0]['group_id'];
            session('block_id',$block['id']);
            session('block_name',$block['block_name']);
            session('block_qq',$block['qq']);
            session('block_weixin',$block['weixin']);
            session('info',json_encode($block['info']));
            session('group_id',$group_id);
            session('building',$block['info']['lists'][$group_id][0]['building']);
        }
        $this->redirect('index/index/index');
    }

    public function index()
    {

        $data=input();
        $data=array_filter($data);
        if(isset($data['group_id'])){
            session('group_id',$data['group_id']);
        }
        $group_id=session('group_id');
        $info=json_decode(session('info'),true);
        $groups=$info['groups'];
        $buildings=$info['lists'][$group_id];
        $buildings=arraySequence($buildings,'building');
        if(isset($data['building'])){
         session('building',$data['building']);
     }
     $building= session('building');
     $status=isset($data['status'])?$data['status']:'';
     
     $this->assign([
        'build' => $building,
        'group_id' => $group_id,
        'status' => $status,
        'groups' => $groups,
        'buildings' => $buildings,
        'cpage' =>'index',
    ]);
     $map['group_id']=$group_id;
     $map['building']=$building;
     $map['status']=$status;
     $map['block_id']=session('block_id');
     $map=array_filter($map);
     $rows=Owner::where($map)->select();
     $this->assign('rows',$rows);
        //统计数据
        //楼盘总户数，楼盘入群数
        //本期总户数，本期入群数
        //本楼总户数，本楼入群数
     $count_map=['block_id' => session('block_id')];
     $tot_sum=db('owner')->where($count_map)->count();
     $count_map['status']=1;
     $tot_in=db('owner')->where($count_map)->count();

     unset($count_map['status']);
     $building2=isset($data['building'])?$data['building']:'';
     $count_map['group_id']=$group_id;
     $count_map['building']=$building2;
     $group_build_sum=db('owner')->where(array_filter($count_map))->count();
     $count_map['status']=1;
     $group_build_in_sum=db('owner')->where(array_filter($count_map))->count();
     $this->assign([
        'tot_sum' => $tot_sum,
        'tot_in' => $tot_in,
        'group_build_sum' => $group_build_sum,
        'group_build_in_sum' => $group_build_in_sum,
        'building2' => $building2,
    ]);
     return view(); 
 }

 public function login(){
    if(request()->isPost()){
        $data=input('post.');
            // dump($data);die;
            // $manager=new Manager;
        $rst=Manager::with('block')->where($data)->find();
            // dump($rst);die;
        if($rst){
            $group_id =session('group_id');
            $building=session('building');
            session(null);
            session('manager_id',$rst['id']);
            session('manager_name',$rst['name']);
            session('manager_block_id',$rst['block_id']);
            session('block_id',$rst['block_id']);
            session('block_name',$rst->block->block_name);
            session('block_qq',$rst->block->qq);
            session('block_weixin',$rst->block->weixin);
            session('group_id',$group_id);
            session('building',$building);
                //查询楼盘楼栋信息，如果有直接读取，如果没有则生成
                // dump($rst->block->info);die;
            if(!$rst->block->info){
                $map=[
                    'block_id' => $rst['block_id'],
                ];
                $rows=Owner::where($map)->distinct(true)->field('group_id,group_name,building')->select();
                $rows=collection($rows)->toArray();
                $groups=Owner::where($map)->distinct(true)->field('group_id,group_name')->select();
                $groups=collection($groups)->toArray();
                foreach($rows as $v){
                    $lists[$v['group_id']][]=$v;
                }
                $info=[
                    'groups' => $groups,
                    'lists' => $lists,
                ];
                $block= Block::get($rst['block_id']);
                $block->info=$info;
                $block->save();
                session('info',json_encode($info));
            }else{
                session('info',json_encode($rst->block->info));
            }
            $this->success('登录成功', 'index/pass','',1);
        }else{
           $this->error('登录失败', 'index/login','',1);
       }
   }else{
    $this->assign('cpage','login');
    return view();
}
}

public function logout(){
    session(null);
    $this->success('退出成功', 'index/login','',1);
}

    //修改管理员资料
public function modifyManager(){
    if(request()->isPost()){
        $data=input();
        if($data['password']!=$data['repassword']){
            $this->error('两次填写的密码不一致', 'index/modifyManager','',1);
        }
        unset($data['repassword']);
        $data=array_filter($data);
        $rst=Manager::update($data);
        if($rst){
            $this->success('修改密码成功', 'index/pass','',1);
        }else{
           $this->error('修改密码失败', 'index/modifyManager','',1);
       }
   }else{
    $id=session('manager_id');
    $row=Manager::get($id);
    $this->assign('row',$row);
    $this->assign('cpage','login');
    return view();
}
}

public function pass()
{
    if(session('manager_id') && session('block_id')==session('manager_block_id')){
      
       $data=input();
       $data=array_filter($data);
       if(isset($data['group_id'])){
        session('group_id',$data['group_id']);
    }
    $group_id=session('group_id');
    $info=json_decode(session('info'),true);
    $groups=$info['groups'];
    $buildings=$info['lists'][$group_id];
    $buildings=arraySequence($buildings,'building');
    if(isset($data['building'])){
       session('building',$data['building']);
   }
   $building= session('building');
   $status=isset($data['status'])?$data['status']:'';

       $this->assign([
        'build' => $building,
        'group_id' => $group_id,
        'status' => $status,
        'groups' => $groups,
        'buildings' => $buildings,
        'cpage' => 'pass',
    ]);
       $map['group_id']=$group_id;
       $map['building']=$building;
       $map['status']=$status;
       $map['block_id']=session('block_id');
       $map=array_filter($map);
       $rows=Owner::where($map)->select();
       $this->assign('rows',$rows);
        //统计数据
        //楼盘总户数，楼盘入群数
        //本期总户数，本期入群数
        //本楼总户数，本楼入群数
       $count_map=['block_id' => session('block_id')];
       $tot_sum=db('owner')->where($count_map)->count();
       $count_map['status']=1;
       $tot_in=db('owner')->where($count_map)->count();

       unset($count_map['status']);
       $building2=isset($data['building'])?$data['building']:'';
       $count_map['group_id']=$group_id;
       $count_map['building']=$building2;
       $group_build_sum=db('owner')->where(array_filter($count_map))->count();
       $count_map['status']=1;
       $group_build_in_sum=db('owner')->where(array_filter($count_map))->count();
       $this->assign([
        'tot_sum' => $tot_sum,
        'tot_in' => $tot_in,
        'group_build_sum' => $group_build_sum,
        'group_build_in_sum' => $group_build_in_sum,
        'building2' => $building2,
    ]);
       return view(); 
   }else{
    $this->error('请先登录再进行操作', 'index/login','',1);
}

}
public function simplenew(){
    
    if(!session('block_id') && !session('manager_id')){
        $alias=input('alias');
        if(!$alias){
            $alias='xj';
        }
        $block=Block::where(['alias' => $alias])->find();
        if(!$block){
            $block=Block::get(1);
        }
        $block=$block->toArray();
        $group_id = $block['info']['groups'][0]['group_id'];
        session('block_id',$block['id']);
        session('block_name',$block['block_name']);
        session('block_qq',$block['qq']);
        session('block_weixin',$block['weixin']);
        session('info',json_encode($block['info']));
        session('group_id',$group_id);
        session('building',$block['info']['lists'][$group_id][0]['building']);
    }

$data=input();
$data=array_filter($data);
if(isset($data['group_id'])){
    session('group_id',$data['group_id']);
}
$group_id=session('group_id');
$info=json_decode(session('info'),true);
$groups=$info['groups'];
$buildings=$info['lists'][$group_id];
$buildings=arraySequence($buildings,'building');
if(isset($data['building'])){
 session('building',$data['building']);
}
$building= session('building');
$status=isset($data['status'])?$data['status']:'';



$this->assign([
'build' => $building,
'group_id' => $group_id,
'status' => $status,
'groups' => $groups,
'buildings' => $buildings,
'cpage' => 'simpleNew',
]);
$map['group_id']=$group_id;
$map['building']=$building;
$map['status']=$status;
$map['block_id']=session('block_id');
$map=array_filter($map);
$temp=Owner::where($map)->select();
if($temp){
 foreach ($temp as $val){
  $rows[$val['floor']][]=$val;
  $rows[$val['floor']]['floor']=$val['floor'];
}
}else{
$rows=[];
}
$this->assign('rows',$rows);

    //统计数据
    //楼盘总户数，楼盘入群数
    //本期总户数，本期入群数
    //本楼总户数，本楼入群数
$count_map=['block_id' => session('block_id')];
$tot_sum=db('owner')->where($count_map)->count();
$count_map['status']=1;
$tot_in=db('owner')->where($count_map)->count();

unset($count_map['status']);
$building2=isset($data['building'])?$data['building']:'';
$count_map['group_id']=$group_id;
$count_map['building']=$building2;
$group_build_sum=db('owner')->where(array_filter($count_map))->count();
$count_map['status']=1;
$group_build_in_sum=db('owner')->where(array_filter($count_map))->count();
$this->assign([
'tot_sum' => $tot_sum,
'tot_in' => $tot_in,
'group_build_sum' => $group_build_sum,
'group_build_in_sum' => $group_build_in_sum,
'building2' => $building2,
]);
return view(); 
}

public function simple($cpage='simple')
{
    if(!session('block_id') && !session('manager_id')){
            $alias=input('alias');
            if(!$alias){
                $alias='xj';
            }
            $block=Block::where(['alias' => $alias])->find();
            if(!$block){
                $block=Block::get(1);
            }
            $block=$block->toArray();
            $group_id = $block['info']['groups'][0]['group_id'];
            session('block_id',$block['id']);
            session('block_name',$block['block_name']);
            session('block_qq',$block['qq']);
            session('block_weixin',$block['weixin']);
            session('info',json_encode($block['info']));
            session('group_id',$group_id);
            session('building',$block['info']['lists'][$group_id][0]['building']);
        }

    $data=input();
    $data=array_filter($data);
    if(isset($data['group_id'])){
        session('group_id',$data['group_id']);
    }
    $group_id=session('group_id');
    $info=json_decode(session('info'),true);
    $groups=$info['groups'];
    $buildings=$info['lists'][$group_id];
    $buildings=arraySequence($buildings,'building');
    if(isset($data['building'])){
     session('building',$data['building']);
 }
 $building= session('building');
 $status=isset($data['status'])?$data['status']:'';


 
 $this->assign([
    'build' => $building,
    'group_id' => $group_id,
    'status' => $status,
    'groups' => $groups,
    'buildings' => $buildings,
    'cpage' => $cpage,
]);
 $map['group_id']=$group_id;
 $map['building']=$building;
 $map['status']=$status;
 $map['block_id']=session('block_id');
 $map=array_filter($map);
 $temp=db('owner')->where($map)->field('floor,status,appartment,manager_name')->select();
 if($temp){
     foreach ($temp as $val){
      $rows[$val['floor']][]=$val;
      $rows[$val['floor']]['floor']=$val['floor'];
  }
}else{
    $rows=[];
}
$this->assign('rows',$rows);

        //统计数据
        //楼盘总户数，楼盘入群数
        //本期总户数，本期入群数
        //本楼总户数，本楼入群数
$count_map=['block_id' => session('block_id')];
$tot_sum=db('owner')->where($count_map)->count();
$count_map['status']=1;
$tot_in=db('owner')->where($count_map)->count();

unset($count_map['status']);
$building2=isset($data['building'])?$data['building']:'';
$count_map['group_id']=$group_id;
$count_map['building']=$building2;
$group_build_sum=db('owner')->where(array_filter($count_map))->count();
$count_map['status']=1;
$group_build_in_sum=db('owner')->where(array_filter($count_map))->count();
$this->assign([
    'tot_sum' => $tot_sum,
    'tot_in' => $tot_in,
    'group_build_sum' => $group_build_sum,
    'group_build_in_sum' => $group_build_in_sum,
    'building2' => $building2,
]);
return view(); 
}

public function passcal(){
   $id=input('id');
   $status=input('status');
   $row=Owner::get($id);
   $row->status=$status;
   
   if($status==1){
      $msg="业主信息验证通过";
  }else{
      $msg="取消业主验证信息";
      $row->update_time=null;
  }
  $row->manager_id=session('manager_id');
  $row->manager_name=session('manager_name');
  $row->save();
  $backdata= ['code' =>1,'msg' =>$msg];
  return $backdata;
}

public function getAppartment(){
 $str='';
 for($a=29;$a>=1;$a--){
   for($b=1;$b<=8;$b++){
      $num=$a.'0'.$b;
      $str="(1,'金桂苑',3,'".$num."'),"."\r\n";
      echo $str;
  }
}
}

public function getFloor(){
    $rows=Owner::where('floor',0)->select();
    foreach($rows as $row){
      $appart=$row['appartment'];
      if(strlen($appart)==4){

         $floor=substr($appart,0,2);
     }else{
         $floor=substr($appart,0,1);
     }
     $row['floor']=$floor;
     $row->save();
 }
 echo "完成";
}

// public function doInsert(){
//     $this->insert(10,'',34,6);
// }

private function insert($build,$type,$floor,$maxunit){
    $arr=[
        'block_id' => 1,
        'group_id' => 2,
        'group_name' => '泰和璟苑',
        'building' => $build,
        'floor' => '',
        'appartment' => '',
        'type' => $type,
        'update_time' => null,
    ];
    $arrs=[];
    $str='';
    for($a=$floor;$a>=2;$a--){
            // if($a%2==1){
        for($b=$maxunit;$b>=1;$b--){
            $num=$a.'0'.$b;
            $arr['appartment']=$num;
            $arr['floor']=$a;
            array_push($arrs,$arr);
        }
            // }
    }
    //    dump($arrs);die;
    //    $owner=new Owner;
    //    $rst=$owner->saveAll($arrs);
    //    if($rst){
    //     echo $build."号楼导入数据成功";
    //     dump($arrs);
    //    }else{
    //     echo $build."号楼导入数据失败";
    //    }
}

public function updateGroup(){
   $block=Block::get(1);
   $groups=$block['info']['groups'];

   $newGroup=[
    "group_id" => 2,
    "group_name" =>"泰和璟苑"
];
array_push($groups,$newGroup);

$lists=$block['info']['lists'];

$newList=[
  ['group_id' => 2,
  'group_name'=> "泰和璟苑",
  'building' => 1
],
['group_id' => 2,
'group_name'=> "泰和璟苑",
'building' => 3
],
['group_id' => 2,
'group_name'=> "泰和璟苑",
'building' => 4
],
['group_id' => 2,
'group_name'=> "泰和璟苑",
'building' => 5
],
];
$lists[2] = $newList;
$newInfo = [
    'groups' => $groups,
    'lists' => $lists
];
      // $block->info = $newInfo;
      // $block->save();
       // dump($block['info']);
}

public function batinsert(){
    // $row=Owner::get(1753);
    // $row->update_time=null;
    // $row->save();
        // $this->insert(48,'六叠',5,4);
        // $this->insert(6,'',34,8);
        // $this->insert(7,'',34,8);
        // $this->insert(9,'',34,8);
        // $this->insert(49,'六叠',5,4);
        // $this->insert(52,'六叠',5,4);
        // $this->insert(53,'六叠',5,4);
        // $this->insert(58,'六叠',5,4);
}

public function jump(){
    session(null);
    $alias=input('alias');
    $this->redirect('index/index/enter',['alias' =>$alias]);

}

    // public function updateNull(){
    //     $row = Owner::where('id',">=",'2749')->update(['update_time' => null]);
    //     dump($row);
    // }


}
